namespace Crux.Core.Domains;

/// <summary>
/// EventHandlerInvoker
/// </summary>
public class EventHandlerInvoker
    : IEventHandlerInvoker
{
    #region implements IEventHandlerInvoker
    /// <inheritdoc/>
    public void Invoke(object eventData, IEnumerable<IEventHandler> eventHandlers)
    {
        foreach (var eventHandler in eventHandlers)
        {
            if (eventHandler is null) continue;

            var interfaces = eventHandler.GetType().GetInterfaces();
            if (interfaces.All(i => i != typeof(IEventHandler))) continue;

            var _ = DynamicInvoker.Invoke(eventHandler, "Handle", [eventData]);
        }
    }

    #endregion implements IEventHandlerInvoker
}